package com.jic.interaction.weChatCardAndOffers.common.utils;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author bourne
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RestResult<T> implements Serializable {

    private static final String SUCCESS_CODE = "8888";
    private static final String SUCCESS_MESSAGE = "请求成功!!!";
    private static final String ERROR_MESSAGE = "请求失败!!!";

    private String ret_code;//返回代码，⻓度为8，最后4位为0时表示成功
    private String ret_msg;
    private String token_status;
    private T ret_data;

    public static RestResult error(String code) {
        return RestResult.builder()
                .ret_code(code)
                .ret_data("")
                .ret_msg(ERROR_MESSAGE)
                .token_status("ok")
                .build();
    }

    public static <T> RestResult<T> success(T data) {
        return RestResult.<T>builder()
                .ret_code(SUCCESS_CODE)
                .ret_data(data)
                .ret_msg(SUCCESS_MESSAGE)
                .token_status("ok")
                .build();
    }

    public static boolean checkIsSuccess(RestResult restResult) {
        if (restResult != null && SUCCESS_CODE.equals(restResult.getRet_code())) {
            return true;
        }
        return false;
    }
}
